package com.gzz.orchid.common.core.tuple;

import com.gzz.orchid.common.core.util.I18nUtil;
import lombok.Value;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 日期时间范围
 *
 * @author higmd
 */
@Value
public class DateTimeRange {
    LocalDateTime begin;
    LocalDateTime end;
    // 构造函数
    public DateTimeRange(LocalDateTime begin, LocalDateTime end) {
        if (Objects.isNull(begin)) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("开始时间不能空为空"));
        }
        if (Objects.isNull(end)) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("结束时间不能空为空"));
        }
        if (!begin.isAfter(end)) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("结束时间必须在开始时间之前"));
        }
        this.begin = begin;
        this.end = end;
    }

    /**
     * 判断日期时间是否在范围之内
     */
    public boolean isTimeBetween(LocalDateTime t) {
        return this.begin.isBefore(t) && this.end.isAfter(t);
    }
}
